package offer.algorithm;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Description：两个队列 实现 一个 栈结构
 * Author: zhangc
 * Date：2017/7/18 16:47
 */
public class QueueToStack7<T> {

    private Queue<T> leftQueue = new LinkedList<>();
    private Queue<T> rightQueue = new LinkedList<>();
    private boolean rightPop = true;

    public  void push(T val){
        if(rightPop)
            leftQueue.add(val);
        else
            rightQueue.add(val);
    }

    public T pop(){
        T retVal = null;
        if (rightPop){
            retVal = transQueue(leftQueue,rightQueue);
        }else {
            retVal = transQueue(rightQueue,leftQueue);
        }
        rightPop = !rightPop;
        return retVal;
    }

    public T transQueue(Queue<T> src,Queue<T> dest){
        if (src.isEmpty())
            return null;
        int size = src.size();
        for (int i = 0; i < size-1; i++) {
            dest.add(src.remove());
        }
        return src.remove();
    }

}
